package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kpx.app.R;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.ShareContent;
import com.kpx.app.cache.FileCache;
import com.kpx.app.configs.Constants;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.HttpDataRequest;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.UserInfoUtils;
import com.kpx.app.utils.systemutils.SystemUtil;
import com.kpx.app.utils.uiutils.QREncoder;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by congdy on 17/3/30.
 * 我的推广－－邀请好友
 */

public class ActivityInvaliteFriend extends ActivityBase implements PlatformActionListener {
    @BindView(R.id.titleview)
    TitleView titleview;
    @BindView(R.id.txt_invalite_price)
    TextView txtInvalitePrice;
    @BindView(R.id.txt_to_invalite)
    TextView txtToInvalite;
    @BindView(R.id.img_qr)
    ImageView imgQr;
    @BindView(R.id.ll_qr)
    LinearLayout llQr;
    @BindView(R.id.img_wx)
    ImageView imgWx;
    @BindView(R.id.img_wxzone)
    ImageView imgWxzone;
    @BindView(R.id.img_qq)
    ImageView imgQq;
    @BindView(R.id.img_qzone)
    ImageView imgQzone;
    @BindView(R.id.img_qr_show)
    ImageView imgQrShow;
    @BindView(R.id.rl_share)
    RelativeLayout rlShare;
    private ShareContent s;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_invalite_friends);
        ButterKnife.bind(this);
        UIUtils.setNormalTitle(titleview, "立即邀请");
        titleview.setBackClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (llQr.getVisibility() == View.VISIBLE) {
                    llQr.setVisibility(View.GONE);
                    return;
                }
                finish();
            }
        });
        llQr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                llQr.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (llQr.getVisibility() == View.VISIBLE) {
            llQr.setVisibility(View.GONE);
            return;
        }
        if (rlShare.getVisibility() == View.VISIBLE) {
            rlShare.setVisibility(View.GONE);
            return;
        }
        super.onBackPressed();
    }

    @Override
    public void initData() {
        s = (ShareContent) FileCache.sharedInstance().get(Constants.KEY_SHARE_BEAN);
        showLoadingDialog();
        MyHashMap map = new MyHashMap();
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                dissmissLoadingDialog();
                if (result.getCode() == 0) {
                    ShareContent s = (ShareContent) result.getData();
                    if (s != null) {
                        s.setTargetUrl(s.getTargetUrl() + "?phone=" + UserInfoUtils.getCachedLoginUser().getUSER().getPhone());
                        ActivityInvaliteFriend.this.s = s;
                        FileCache.sharedInstance().put(Constants.KEY_SHARE_BEAN, s);
                    }
                } else {
                    ToastUtil.getInstance().showToast(ActivityInvaliteFriend.this, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                dissmissLoadingDialog();
                ToastUtil.getInstance().showToast(ActivityInvaliteFriend.this, error);
            }

        }, ShareContent.class, HttpDataRequest.TYPE_OBJECT).POST(HttpDataReqUrl.URL_POST_GET_SHARE_INFO, true, map);

    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }
//
//    @OnClick()
//    public void onClick() {
//        ShareSDKUtils.getInstance().setListener(new ShareSDKUtils.OnShareListener() {
//            @Override
//            public void onShareSuccessListener() {
//                ToastUtil.getInstance().showToast(ActivityInvaliteFriend.this, "分享成功");
//            }
//
//            @Override
//            public void onShareFailListener() {
//                ToastUtil.getInstance().showToast(ActivityInvaliteFriend.this, "分享失败");
//            }
//        });
//        if (s != null) {
//            ShareSDKUtils.getInstance().share(s, ActivityInvaliteFriend.this);
//        } else {
//            ToastUtil.getInstance().showToast(ActivityInvaliteFriend.this, "分享失败");
//        }

//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.img_wx, R.id.img_wxzone, R.id.img_qq, R.id.img_qzone, R.id.img_qr_show, R.id.rl_share, R.id.txt_to_invalite})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_wx:
                shareWx();
                break;
            case R.id.img_wxzone:
                shareWxzone();
                break;
            case R.id.img_qq:
                shareQQ();
                break;
            case R.id.img_qzone:
                shareQQzone();
                break;
            case R.id.img_qr_show:
                rlShare.setVisibility(View.GONE);
                llQr.setVisibility(View.VISIBLE);
                if (s != null) {
                    QREncoder.generateQRCodeImage(s.getTargetUrl(), imgQr, 180);
                } else {
                    ToastUtil.getInstance().showToast(ActivityInvaliteFriend.this, "获取失败");
                }
                break;
            case R.id.rl_share:
                rlShare.setVisibility(View.GONE);
                break;
            case R.id.txt_to_invalite:
                rlShare.setVisibility(View.VISIBLE);
                break;
        }
    }

    private void shareWx() {
        if (!SystemUtil.isPkgInstalled(this, "com.tencent.mm")) {
            ToastUtil.getInstance().showDialog(this, getString(R.string.alert_install_wechat));
            return;
        }
        ShareSDK.initSDK(this);
        Wechat.ShareParams sp = new Wechat.ShareParams();
        if(sp==null)return;
        sp.setTitle(s.getTitle());
        sp.setTitleUrl(s.getTargetUrl()); // 标题的超链接
        sp.setText(s.getContent());
        sp.setImageUrl(s.getImgs());
        sp.setSite(s.getTitle());
        sp.setSiteUrl(s.getTargetUrl());
        sp.setImageUrl(s.getImgs());
        sp.setUrl(s.getTargetUrl());
        sp.setShareType(Platform.SHARE_WEBPAGE);
        Platform qzone = ShareSDK.getPlatform(Wechat.NAME);
        qzone.setPlatformActionListener(this);
        qzone.share(sp);
    }

    private void shareWxzone() {
        if (!SystemUtil.isPkgInstalled(this, "com.tencent.mm")) {
            ToastUtil.getInstance().showDialog(this, getString(R.string.alert_install_wechat));
            return;
        }
        ShareSDK.initSDK(this);
        WechatMoments.ShareParams sp = new WechatMoments.ShareParams();
        if(sp==null)return;
        sp.setTitle(s.getTitle());
        sp.setTitleUrl(s.getTargetUrl()); // 标题的超链接
        sp.setText(s.getContent());
        sp.setImageUrl(s.getImgs());
        sp.setSite(s.getTitle());
        sp.setSiteUrl(s.getTargetUrl());
        sp.setImageUrl(s.getImgs());
        sp.setUrl(s.getTargetUrl());
        sp.setShareType(Platform.SHARE_WEBPAGE);
        Platform qzone = ShareSDK.getPlatform(WechatMoments.NAME);
        qzone.setPlatformActionListener(this);
        qzone.share(sp);
    }

    private void shareQQ() {
        ShareSDK.initSDK(this);
        QQ.ShareParams sp = new QQ.ShareParams();
        if(sp==null)return;
        sp.setTitle(s.getTitle());
        sp.setTitleUrl(s.getTargetUrl()); // 标题的超链接
        sp.setText(s.getContent());
        sp.setImageUrl(s.getImgs());
        sp.setSite(s.getTitle());
        sp.setSiteUrl(s.getTargetUrl());

        Platform qzone = ShareSDK.getPlatform(QQ.NAME);
        qzone.setPlatformActionListener(this);
        qzone.share(sp);
    }

    private void shareQQzone() {
        ShareSDK.initSDK(this);
        QZone.ShareParams sp = new QZone.ShareParams();
        if(sp==null)return;
        sp.setTitle(s.getTitle());
        sp.setTitleUrl(s.getTargetUrl()); // 标题的超链接
        sp.setText(s.getContent());
        sp.setImageUrl(s.getImgs());
        sp.setSite(s.getTitle());
        sp.setSiteUrl(s.getTargetUrl());

        Platform qzone = ShareSDK.getPlatform(QZone.NAME);
        qzone.setPlatformActionListener(this);
        qzone.share(sp);
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        ToastUtil.getInstance().showToast(this, "分享成功");
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {

        ToastUtil.getInstance().showToast(this, "分享失败");
    }

    @Override
    public void onCancel(Platform platform, int i) {

        ToastUtil.getInstance().showToast(this, "分享取消");
    }
}
